import Vue from 'vue'
import Vuex from 'vuex'
import router from '../router'
import { Dialog } from 'vant'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    isLogin: localStorage.getItem('isLogin') === 'true' || false
  },
  mutations: {
    changeLoginState(state, payload){
      state.isLogin = payload
    }
  },
  actions: {
    async onSubmit (context, params) {
      const loginPass = await login({ loginname: params.username, password: params.password })
      console.log(loginPass)
      console.log(loginPass.data.message)
      if (loginPass.data.code === '10010') {
        // console.log('该用户不存在')
        Dialog.confirm({
          message: '该用户不存在,是否跳转注册页面'
        })
          .then(() => {
            this.$router.push('/step0')
          })
          .catch(() => {
          })
      } else if (loginPass.data.code === '10011') {
        // console.log('密码错误')
        Toast('密码错误')
      } else {
        // console.log('密码正确')
        Toast('密码正确')
        localStorage.setItem('userid', loginPass.data.data.userid)
        localStorage.setItem('token', loginPass.data.data.token)
        localStorage.setItem('isLogin', true)
        router.go(-1)
        context.commit('changeLoginState', true)
      }
    }
  },
  modules: {
  }
})
